package com.heima.Control;

import com.heima.poji.Result;
import com.heima.utils.AliOSSUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController
@Slf4j
public class UpLoadContral {
    @Autowired
    private AliOSSUtils aliOSSUtils;
//    @PostMapping("/upload")
//    public Result Upload(String username, Integer age, MultipartFile image) throws  Exception {
//        log.info("这是图片上传");
//        //spring.servlet.multipart.max-file-size=10MB;
//        //获取原始文件名
//        String originalFilename = image.getOriginalFilename();
//        //构建新的文件名
//        String extname =
//                originalFilename.substring(originalFilename.lastIndexOf("."));//文件扩展名
//        String newFileName = UUID.randomUUID().toString()+extname;//随机名+文件扩展名
////将文件存储在服务器的磁盘目录
//        image.transferTo(new File("C:/Users/22973/Desktop/images/"+newFileName));
//        //image.transferTo(new File("C:/Users/22973/Desktop/images/"+originalFilename));
//        return Result.success();
//    }
    @PostMapping("/upload")
    public Result imageUpload(MultipartFile image) throws Exception {
          String url = aliOSSUtils.upload(image);
        return Result.success(url);
    }
}
